@import org.silkframework.workspace.User
@import org.silkframework.workspace.Constants
@import org.silkframework.dataset.Dataset
@import org.silkframework.util.DPair
@import controllers.rules.routes.Assets
@import org.silkframework.rule.LinkSpec
@import org.silkframework.rule.TransformSpec
@import org.silkframework.rule.LinkSpec
@(project: String, task: String)

@datasets = @{ User().workspace.project(project).tasks[Dataset] }

@transformTasks = @{ User().workspace.project(project).tasks[TransformSpec] }

@linkingTask = @{ User().workspace.project(project).tasks[LinkSpec].find(_.id == task) }

@currentInputs = @{ linkingTask.map(_.data.dataSelections) }

@currentOutput = @{ linkingTask.flatMap(_.data.outputs.headOption) }

@prefixes = @{ User().workspace.project(project).config.prefixes }

@widgets.dialog(title = "Linking Task") {
  <div class="dialog-form" id="linkingTaskDialogForm">
    @if(task.isEmpty) {
      <div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
        <input class="mdl-textfield__input" type="text" id="linking_task_name" name="linking_task_name"/>
        <label class="mdl-textfield__label" for="linking_task_name">Name</label>
      </div>
    }
    @if(currentInputs.isDefined) {
      @widgets.datasetSelect(project, "source", Some(currentInputs.get.source.inputId))
    } else {
      @widgets.datasetSelect(project, "source", None)
    }
    <div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
      <input class="mdl-textfield__input" type="text" id="source_type" name="source_type" @for(t <- linkingTask) { value="@t.data.dataSelections.source.typeUri.serialize(prefixes)" }/>
      <label class="mdl-textfield__label" for="source_type">Source Type</label>
      <div class="mdl-tooltip mdl-tooltip--right" for="source_type">
        The type of entities to be retrieved from the source dataset.
      </div>
    </div>
    @widgets.restriction("source", currentInputs.map(_.source.restriction.serialize).getOrElse(""))
    @if(currentInputs.isDefined) {
      @widgets.datasetSelect(project, "target", Some(currentInputs.get.target.inputId))
    } else {
      @widgets.datasetSelect(project, "target", None)
    }
    <div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
      <input class="mdl-textfield__input" type="text" id="target_type" name="target_type" @for(t <- linkingTask) { value="@t.data.dataSelections.target.typeUri.serialize(prefixes)" }/>
      <label class="mdl-textfield__label" for="target_type">Target Type</label>
      <div class="mdl-tooltip mdl-tooltip--right" for="target_type">
        The type of entities to be retrieved from the target dataset.
      </div>
    </div>
    @widgets.restriction("target", currentInputs.map(_.target.restriction.serialize).getOrElse(""))
    <div class="mdl-selectfield mdl-js-selectfield mdl-selectfield--floating-label">
      <select class="mdl-selectfield__select" id="output_dataset" name="output_dataset">
        <option value=""></option>
        @for(dataset <- datasets) {
          @if(currentOutput.isDefined && dataset.id == currentOutput.get) {
            <option value="@dataset.id" selected="selected">@dataset.id</option>
          } else {
            <option value="@dataset.id">@dataset.id</option>
          }
        }
      </select>
      <label class="mdl-selectfield__label" for="output_dataset">Output</label>
      <div class="mdl-tooltip mdl-tooltip--right" for="output_dataset">
        The generated links are written to this dataset.
        If not specified, the links are only cached locally.
      </div>
    </div>
  </div>
}

<script type="text/javascript">

  // The variable name.
  var varName;
  // The name of the currently edited datasource.
  var sourceName;

  $(function() {
    // Add autocompletion
    $("input[name='source_type']").autocomplete({
      appendTo: "#linkingTaskDialogForm",
      source: function( request, response ) {
        var dataset = $("select[name='source_dataset']").val();
        $.get('@config.baseUrl/workspace/datasets/@project/' + dataset + '/types', { search: request.term }, function(data) {
          response(data);
        });
      },
      minLength: 0
    }).focus(function() { $(this).autocomplete("search"); });

    $("input[name='target_type']").autocomplete({
      appendTo: "#linkingTaskDialogForm",
      source: function( request, response ) {
        var dataset = $("select[name='target_dataset']").val();
        $.get('@config.baseUrl/workspace/datasets/@project/' + dataset + '/types', { search: request.term }, function(data) {
          response(data);
        });
      },
      minLength: 0
    }).focus(function() { $(this).autocomplete("search"); });
  });

  function submit() {
    var values = {
      name: @if(!task.isEmpty) {'@task'} else {$("#linking_task_name").val()},
      source: $("#source_dataset").val(),
      sourceType: $("#source_type").val(),
      sourceRestriction: $("#source_restriction_textarea").val(),
      target: $("#target_dataset").val(),
      targetType: $("#target_type").val(),
      targetRestriction: $("#target_restriction_textarea").val(),
      output: $("#output_dataset").val()
    };

    if(values.name.length === 0) {
      alert("Task name is empty.");
      return;
    }

    $.ajax({
      type: 'PUT',
      url: '@config.baseUrl/linking/tasks/@project/' + values.name,
      data: values,
      success: function(data) {
        closeDialog();
        reloadWorkspace();
      },
      error: function(request) {
        alert(request.responseText);
      }
    });
  }
</script>
